<template>
  <div class="login">
    <el-form ref="form" :model="loginForm" :rules="loginRules" class="login-form" label-width="70px">
      <h2 class="title">后台管理系统</h2>
      <el-form-item label="用户名" prop="username">
        <el-input v-model="loginForm.username" auto-complete="off" link>
          <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input ref="password-input" v-model="loginForm.password" :readonly="isPasswordReadonly" auto-complete="off"
                  type="password" @focus="onPasswordFocus()" @keyup.enter.native="handleLogin">
          <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item v-if="captchaEnabled" label="验证码" prop="code">
        <el-input v-model="loginForm.code" auto-complete="off" style="width: 63%" @keyup.enter.native="handleLogin">
          <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
        </el-input>
        <div class="login-code">
          <img :src="codeUrl" class="login-code-img" @click="getCode"/>
        </div>
      </el-form-item>
      <!-- <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 70px;">记住密码</el-checkbox> -->
      <el-form-item style="width:100%;">
        <el-button :loading="loading" size="default" style="width:100%;" type="primary"
                   @click.native.prevent="handleLogin">
          <!--          ["", "default", "small", "large"]-->
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
        <div v-if="register" style="float: right;">
          <router-link :to="'/register'" class="link-type">立即注册</router-link>
        </div>
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>Copyright © 2018-2022 zpTest.vip All Rights Reserved.</span>
    </div> -->
  </div>
</template>
<script lang="ts" setup>
import {onMounted, ref, reactive} from 'vue'
import type {FormRules} from 'element-plus'
import {getCodeImg} from "@/api/login"
import {useRouter} from 'vue-router'

import {useUserStore} from "@/store/modules/user";

const userStore = useUserStore();
const router = useRouter()

onMounted(() => {
  getCode()
})


const codeUrl = ref("")
const isPasswordReadonly = ref(true)
const loading = ref(false)
// 验证码开关
const captchaEnabled = ref(false)
// 注册开关
const register = ref(false)

const redirect = ref(undefined)

const loginForm = reactive({
  username: '',
  password: '',
  rememberMe: false,
  code: '',
  uuid: ''
})

const form = ref(null)
// 规则验证
const loginRules = reactive<FormRules>({
  username: [
    {
      required: true,
      trigger: 'blur',
      message: '请输入您的账号'
    }
  ],
  password: [
    {
      required: true,
      trigger: 'blur',
      message: '请输入您的密码'
    }
  ],
  code: [{
    required: true,
    trigger: 'change',
    message: '请输入验证码'
  }]
})

const getCode = () => {
  getCodeImg().then(res => {
    //   this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled
    if (captchaEnabled.value) {
      codeUrl.value = 'data:image/gif;base64,' + res.data.img
      loginForm["uuid"] = res.data.uuid
    }
  })
}

const handleLogin = () => {
  (form.value as any).validate((valid: any) => {
        if (!valid) return
        userStore.Login(loginForm).then(() => {
          // eslint-disable-next-line @typescript-eslint/no-empty-function
          router.push({path: redirect.value || '/'})
        }).catch(() => {
          // this.loading = false;
          if (captchaEnabled.value) {
            getCode()
          }
        })
      }
  )
}

const onPasswordFocus = () => {
  isPasswordReadonly.value = false
}
</script>

<style lang="scss">
.login {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/login-background.png");
  background-size: cover;
}

.title {
  // margin: 0px auto 30px auto;
  text-align: center;
  font-weight: 600;
  color: #303133;
}

.login-form {
  // border-radius: 6px;
  // background: #ffffff;
  // width: 400px;
  // padding: 25px 25px 5px 25px;

  width: 510px;
  margin-left: auto;
  margin-top: 15%;
  margin-bottom: 15%;
  margin-right: 20%;
  background-color: rgba(255, 255, 255, 0.8);
  padding: 30px 60px 30px 30px;
  border-radius: 20px;

  .el-input {
    height: 38px;

    input {
      height: 38px;
    }
  }

  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
}

.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}

.login-code {
  width: 33%;
  height: 38px;
  float: right;

  img {
    cursor: pointer;
    vertical-align: middle;
  }
}

.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}

.login-code-img {
  height: 38px;
}
</style>
